package com.vca.admin.controller;

import com.vca.common.page.CommonPage;
import com.vca.common.request.*;
import com.vca.common.response.AdminHeaderResponse;
import com.vca.common.response.CourseAdminListResponse;
import com.vca.common.response.CoursePackageAdminListResponse;
import com.vca.common.response.CoursePackageAdminResponse;
import com.vca.common.result.CommonResult;
import com.vca.service.service.CoursePackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套课控制器
 *
 * @author Li
 * @date 2022/11/17  16:48
 */
@Slf4j
@RestController("CoursePackageController")
@RequestMapping("api/admin/course/package")
@Api(tags = "套课控制器")
public class CoursePackageController {

    @Autowired
    private CoursePackageService coursePackageService;

    /**
     * 套课列表
     * @author Li
     * @date 2022/11/17 17:15
     * @return
     */
    @PreAuthorize("hasAuthority('admin:course:package:list')")
    @ApiOperation(value = "套课列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<CoursePackageAdminListResponse>> getList(@ModelAttribute @Validated AdminCommonRequest request, @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage commonPage = CommonPage.restPage(coursePackageService.getAdminList(request,pageParamRequest));
        return CommonResult.success(commonPage);
    }

    /**
     * 修改排序
     * @param coursePackageId 套课id
     * @param sort 排序
     * @author Li
     * @date    2022/11/18 09:36
     * @return
     */
    @PreAuthorize("hasAuthority('admin:course:package:update:sort')")
    @ApiOperation("修改排序")
    @GetMapping("/updateSort")
    public CommonResult updateSort(Integer coursePackageId,Integer sort){
        return coursePackageService.updateSort(coursePackageId,sort)?CommonResult.success():CommonResult.failed();
    }


    /**
     * 上架课程
     * @param coursePackageId 套课id
     * @author Li
     * @date    2022/11/18 09:36
     * @return
     */
    @PreAuthorize("hasAuthority('admin:course:package:shell:on')")
    @ApiOperation("上架课程")
    @GetMapping("/courseOnShell")
    public CommonResult courseOnShell(Integer coursePackageId){
        return coursePackageService.courseOnShell(coursePackageId)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 下架课程
     * @param coursePackageId 套课id
     * @author Li
     * @date    2022/11/18 09:36
     * @return
     */
    @PreAuthorize("hasAuthority('admin:course:package:shell:off')")
    @ApiOperation("下架课程")
    @GetMapping("/courseOffShell")
    public CommonResult courseOffShell(Integer coursePackageId){
        return coursePackageService.courseOffShell(coursePackageId)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 添加套课
     * @author Li
     * @date    2022/11/18 10:11
     * @return
     */
    @PreAuthorize("hasAuthority('admin:course:package:add')")
    @ApiOperation("添加套课")
    @PostMapping("/add")
    public CommonResult addCoursePackage(@RequestBody CoursePackageAddRequest request){
        return coursePackageService.addCoursePackage(request)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 根据id获取套课信息
     * @param coursePackageId
     * @author Li
     * @date    2022/11/22 16:29
     * @return
     */
    @PreAuthorize("hasAuthority('admin:course:package:get')")
    @ApiOperation("根据id获取套课信息")
    @GetMapping("/getCourse")
    public CommonResult<CoursePackageAdminResponse> getCourse(Long coursePackageId){
        return CommonResult.success(coursePackageService.getCourse(coursePackageId));
    }

    /**
     * @Description:修改套课
     * @Author: Li
     * @Date: 2022/11/22 16:29
     */
    @PreAuthorize("hasAuthority('admin:course:package:update')")
    @ApiOperation(value = "修改套课")
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody CoursePackageAddRequest request) {
        return coursePackageService.updateCourse(request)?CommonResult.success():CommonResult.failed();
    }


    /**
     * 获得课程头部数据
     * @author Li
     * @date 2022/11/23 09:16
     * @return
     */
    @ApiOperation(value = "获得套课头部数据")
    @GetMapping("/header")
    public CommonResult<List<AdminHeaderResponse>> getHeader() {
        return CommonResult.success(coursePackageService.getHeader());
    }

    /**
     * 删除套课
     * @author Li
     * @date 2022/11/23 09:16
     * @return
     */
    @PreAuthorize("hasAuthority('admin:course:package:delete')")
    @ApiOperation(value = "删除套课")
    @GetMapping("/delete")
    public CommonResult delete(Long coursePackageId){
        return coursePackageService.deleteCourse(coursePackageId)?CommonResult.success():CommonResult.failed();
    }

}
